<?php
function n2br($str){
	if ($str!='') {
		return str_replace("\n","<br />",$str);
	}	
}

function getMoney($v){
    if ($v<=0) {
        return '面议';
    }else{
        return $v;
    }
}

function getCateName($v){
    if (!is_numeric($v)) {
      return;die;
   }

   $map['id'] = $v;
   $name = M('Category')->where($map)->getField('name');
   if ($name) {
      return $name;
   }else{
      return 'error';
   }
}

//检查提交数据
function checkRequest(){
    $url = $_SERVER["HTTP_REFERER"]; 
    $str = str_replace("http://","",$url); 
    $strdomain = explode("/",$str); 
    $domain = $strdomain[0]; 
    if ($domain!=$_SERVER['SERVER_NAME']){ //站外提交
        return false;
    }else{
        return checkFormDate();
    }
}

//过滤数据
function checkFormDate(){
    foreach ($_REQUEST as $key => $value){        
        if (inject_check($value)) {
            return false;
            break;
        } 
    }
    return true;
}

function inject_check($sql_str) {  
    return preg_match("/select|inert|script|iframe|update|delete|\'|\/\*|\*|\.\.\/|\.\/|UNION|into|load_file|outfile/i", $sql_str);
} 

function inject_replace($sql_str) {  
    return preg_replace("/select|inert|script|iframe|update|delete|\'|\/\*|\*|\.\.\/|\.\/|UNION|into|load_file|outfile/i","",$sql_str);
} 

//发邮件
function think_send_mail($email, $name, $title, $content) {
    Vendor('PHPMailer.PHPMailerAutoload'); 
    $mail = new PHPMailer(); //实例化
    $mail->IsSMTP(); // 启用SMTP 
    $mail->SMTPDebug = 1; // 关闭SMTP调试功能
    $mail->Host = C('SMTP_HOST'); //smtp服务器的名称
    $mail->Port = 465;
    $mail->SMTPSecure = "ssl"; 
    $mail->SMTPAuth = true; //启用smtp认证
    $mail->Username = C('SMTP_USER'); //你的邮箱名
    $mail->Password = C('SMTP_PASS'); //邮箱密码
    $mail->From = C('FROM_EMAIL'); //发件人地址（也就是你的邮箱地址）
    $mail->FromName = C('FROM_NAME'); //发件人姓名
    $mail->AddAddress($email, $name); //添加收件人
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(true); // 是否HTML格式邮件
    $mail->CharSet = "utf-8"; //设置邮件编码
    $mail->Subject = $title; //主题
    $mail->Body = $content; //内容
    $mail->AltBody = ""; //正文不支持HTML的备用显示
    return $mail->Send();
}


//生成随机字符串
function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
} 


function isMobile() { 
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    } 

    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset($_SERVER['HTTP_VIA'])) { 
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    } 

    // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger'); 
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
        return true;
        } 
    } 
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) { 
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
        return true;
        } 
    } 
    return false;
}
?>